/**
 * Created by Administrator on 2017/8/25 0025.
 */
var userBiz = require('./frame/biz/user-biz'),
    acctBiz = require('./acct/biz/account'),
    todoBiz = require('./acct/biz/todo'),
    logInfoBiz = require('./frame/biz/log-info-biz'),
    router = {
    init: function(app){
        app.get('/login', function(req, res) {
            var objWhere = {
                username: req.param("username"),
                password:req.param("password"),
                ip: getClientIp(req)
            };
            userBiz.findUserByParam(objWhere,function(err,arrUser){
                if(err < 0){
                    res.json({err:err,errMsg: arrUser});
                    return;
                }
                if(arrUser.length > 0){
                    req.session.userInfo = arrUser[0]._doc;
                }else{
                    req.session.userInfo = null;
                }
                res.json(arrUser);
            });
        });

        app.post('/user',function(req,res){
            userBiz.create(req.param("username"),req.param("password"),function(err){
                res.send({status:"success", message:"add user success"});
            });

        });

        app.put('/userPwd',function(req,res){
            var param = {
                username: req.session.userInfo.username,
                password: req.body.newPwd
            };
            userBiz.updatePwd(param).then(function(err){
                res.send({status:"success", message:"add user success"});
            });

        });

        app.post('/account',function(req,res){
            if(req.session.userInfo == null){
                return false;
            }
            req.body.prvKey = req.session.userInfo.prvKey;
            acctBiz.create(req.body,function(err){
                res.send({status:"success", message:"add user success"});
            });

        });

        app.put('/account',function(req,res){
            req.body.prvKey = req.session.userInfo.prvKey;
            acctBiz.update(req.body).then(function(err){
                res.send({status:"success", message:"add user success"});
            });

        });

        app.get('/myAccounts',function(req,res){
            if(req.session.userInfo == null){
                return false;
            }
            var param = {
                prvKey: req.session.userInfo.prvKey
            };
            acctBiz.findUserByParam(param).then(function(arrAcct){
                res.json(arrAcct);
            });

        });

        app.delete('/myAccounts',function (req,res) {
            acctBiz.deleteByParam({id:req.param("id")}).then(function(arrTodo){
                res.json(arrTodo);
            });
        });

        app.get('/logout',function(req,res){
            req.session.userInfo = null;
            res.redirect("/");
        });

        app.post('/todoItem',function (req,res) {
            req.body.creator = req.session.userInfo.username;
            todoBiz.create(req.body).then(function(err){
               res.json({status:"success", message:"add user success"});
            });
        });

        app.get('/todoItem',function (req,res) {
            todoBiz.findByParam({
                creator: req.session.userInfo.username
            }).then(function(arrTodo){
                res.json(arrTodo);
            });
        });

        app.put('/todoItem',function (req,res) {
            todoBiz.update(req.body).then(function(arrTodo){
                res.json(arrTodo);
            });
        });

        app.delete('/todoItem',function (req,res) {
            todoBiz.deleteByParam({id:req.param("id")}).then(function(arrTodo){
                res.json(arrTodo);
            });
        });

        app.get('/sysLog',function (req,res) {
            var param = {date: req.param('date'),ip: req.param('ip')};
            logInfoBiz.findLogByParam(param).then(function (arrLog) {
                res.json(arrLog);
            });
        });
    }
};

function getClientIp(req) {
    return  req.ip ||
        req.headers['x-real-ip'] ||
        req.headers['x-forwarded-for'] ||
        req.connection.remoteAddress ||
        req.socket.remoteAddress ||
        req.connection.socket.remoteAddress;
};

module.exports = router;